package com.hepinghospital.user.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.response.ScheduleListResponse;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by HP on 2020/3/2.
 */

public class OffLineAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<ScheduleListResponse.Schedule> list;
    private OnItemClickLitener mOnItemClickLitener;
    private int Position;


    public OffLineAdapter(Context context, List<ScheduleListResponse.Schedule> list) {
        this.mContext = context;
        this.list = list;
    }


    public void setPosition(int position) {
        this.Position = position;
    }

    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_offline_time, parent, false);
        return new ViewHolder(view);

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder myHolder = (ViewHolder) holder;

        if (list.get(position).flag == 1) {
            myHolder.tvDateTime.setText("时段：" + list.get(position).schTime.substring(0, 5));
        } else if (list.get(position).flag == 0) {
            myHolder.tvDateTime.setText("时段：" + list.get(position).schTime.substring(0, 5));
            //holder.tv_dateTime.setText( list.get( position ).schTime + " 已约" );
        }
        if (Position == position) {
            if (list.get(position).flag == 1) {
                myHolder.tvDateTime.setTextColor(ContextCompat.getColor(mContext, R.color.ff333333));
            } else {
                myHolder.tvDateTime.setTextColor(ContextCompat.getColor(mContext, R.color.ff999999));
            }
        } else {
            if (list.get(position).flag == 1) {
                myHolder.tvDateTime.setTextColor(ContextCompat.getColor(mContext, R.color.commonGrey));
            } else {
                myHolder.tvDateTime.setTextColor(ContextCompat.getColor(mContext, R.color.ffcccccc));
            }
        }

        if (mOnItemClickLitener != null) {
            myHolder.itemView.setOnClickListener(v -> {
                if (list.get(position).flag == 1) {
                    mOnItemClickLitener.onItemClick(myHolder.itemView, position);
                } else if (list.get(position).flag == 0) {
                    //不可点击
                    ToastUtil.showToast(mContext, "请选择其他号源");
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }


    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_number)
        TextView tvNumber;
        @BindView(R.id.tv_dateTime)
        TextView tvDateTime;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
